package com.thinklib.mapper;

import com.thinklib.bean.RoleUnit;
import com.thinklib.bean.TestBean;
import com.thinklib.bean.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface UserMapper {

	// 根据username查询某个用户
	@Select("SELECT * FROM t_user WHERE email=#{email}")
	@Results({
		@Result(column = "id", property = "id"),
		@Result(column = "email", property = "email"),
		@Result(column = "phone", property = "phone"),
		@Result(column = "password", property = "password"),
		@Result(column = "nickname", property = "nickname"),
		@Result(column = "avatar", property= "avatar"),
		@Result(column = "sex", property = "sex"),
		@Result(column = "state", property = "state"),
		@Result(column = "activation_code", property = "activationCode"),
		@Result(column = "create_time", property = "createTime")
	})
	User getUserByUsername(String username);

	// 插入一条用户记录
    @Insert("INSERT INTO t_user(email,phone,password,nickname,avatar,sex,state,activation_code) VALUES(#{email},#{phone},#{password},#{nickname},#{avatar},#{sex},#{state},#{activationCode})")
    // 获取数据库自增ID，keyProperty：java对象的属性，keyColumn：数据库字段名称
    @Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
    int addUser(User user);

    // 激活账号服务
    @Update("update t_user set state = 1 where activation_code=#{code}")
	int activationEmail(String code);

    // 根据activation_code查询某个用户
 	@Select("SELECT * FROM t_user WHERE activation_code=#{code}")
 	@Results({ 
 		@Result(column = "id", property = "id"),
 		@Result(column = "email", property = "email"),
 		@Result(column = "phone", property = "phone"),
		@Result(column = "avatar", property = "avatar"),
 		@Result(column = "password", property = "password"),
 		@Result(column = "nickname", property = "nickname"),
 		@Result(column = "sex", property = "sex"),
 		@Result(column = "state", property = "state"),
 		@Result(column = "activation_code", property = "activationCode"),
 		@Result(column = "create_time", property = "createTime")
 	})
	User findUserByCode(String code);
 	
 	// 查询所有用户
 	@Select("SELECT * FROM t_user")
 	@Results({
 		@Result(column = "id", property = "id"),
 		@Result(column = "email", property = "email"),
 		@Result(column = "phone", property = "phone"),
		@Result(column = "avatar", property = "avatar"),
 		@Result(column = "password", property = "password"),
 		@Result(column = "nickname", property = "nickname"),
 		@Result(column = "sex", property = "sex"),
 		@Result(column = "state", property = "state"),
 		@Result(column = "activation_code", property = "activationCode"),
 		@Result(column = "create_time", property = "createTime")
 	})
 	List<User> findAllUser();

 	
 	// 根据userId查询角色
  	@Select("SELECT * FROM t_role where user_id=#{userId}")
  	@Results({
  		@Result(column = "id", property = "id"),
  		@Result(column = "role_name", property = "roleName"),
  		@Result(column = "role_id", property = "roleId"),
  		@Result(column = "user_id", property = "userId")
  	})
	RoleUnit findRoleByUserId(int userId);

	// 根据userId查询角色
	@Select("SELECT * FROM t_user where email=#{email}")
	@Results({
			@Result(column = "id", property = "id"),
			@Result(column = "email", property = "email"),
			@Result(column = "phone", property = "phone"),
			@Result(column = "avatar", property = "avatar"),
			@Result(column = "password", property = "password"),
			@Result(column = "nickname", property = "nickname"),
			@Result(column = "sex", property = "sex"),
			@Result(column = "state", property = "state"),
			@Result(column = "activation_code", property = "activationCode"),
			@Result(column = "create_time", property = "createTime")
	})
	User findUserByEmail(String email);

	// 插入100万测试数据
	@Insert("INSERT INTO t_test(username,password,remark) VALUES(#{username},#{password},#{remark})")
	// 获取数据库自增ID，keyProperty：java对象的属性，keyColumn：数据库字段名称
	@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
	int addTest(TestBean testBean);
}